﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarolLib.Control;
using System.Xml;
using System.Collections;
using DispatchAssist.Service;
using System.Web.Services.Protocols;
using DispatchAssist.Model;
using CarolLib;

namespace DispatchAssist.AppHost
{
    [ModuleContract("18B3E24E-551F-4418-8B0A-8DAE2204994D", "日志管理", "操作日志查询")]
    public partial class QueryAlertActAndSystemLogManage : MdiUserControl
    {
        AuthService service = new AuthService();

        int previewIndex = 0;
        public QueryAlertActAndSystemLogManage()
            : base()
        {
            InitializeComponent();
        }
        public QueryAlertActAndSystemLogManage(XmlNode xn)
            : this()
        {
        }



        private void BindDataGrid(QueryCondition qc)
        {
            try
            {
                WaitForm wf = new WaitForm();
                wf.StartPosition = FormStartPosition.CenterParent;
                service.QueryLogCommonAsync("ACTANDSYS", qc, wf);
                //service.QueryLogCommon("ACTANDSYS", qc);
                wf.ShowDialog();
            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }

        }
        void service_QueryLogCommonCompleted(object sender, QueryLogCommonCompletedEventArgs e)
        {
            string jsonstr = (string)e.Result;
            List<QueryLoginAndSystemLog> apflst = null;
            try
            {
                apflst = JsonSerializeHelper.DataContractJsonDeserializeByJNet<List<QueryLoginAndSystemLog>>(jsonstr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            if (apflst == null) { ((WaitForm)e.UserState).Close(); return; }
            dgActPerfromList.AutoGenerateColumns = false;

            CarolLib.Control.SortableBindingList<QueryLoginAndSystemLog> sortable = new SortableBindingList<QueryLoginAndSystemLog>(apflst);


            dgActPerfromList.DataSource = sortable;
            ((WaitForm)e.UserState).Close();
        }







        private void BindOperaters()
        {
            // Dictionary<string, string> dic = new Dictionary<string, string>();

            List<SeatUser> users = service.GetAllSeatUser(1).ToList<SeatUser>();

            //foreach (SeatUser su in users)
            //{
            //    dic.Add(su.UserID.ToString(), su.UserName);

            //}

            //users.ToDictionary<SeatUser, string>(s => s.UserID.ToString());
            SeatUser su = new SeatUser();
            su.UserID = new Guid();
            su.UserName = "未选择";
            users.Insert(0, su);
            cmbOperater.DataSource = users == null ? new List<SeatUser>() : users;
            cmbOperater.DisplayMember = "UserName";
            cmbOperater.ValueMember = "UserID";

        }
        public override void Onload()
        {
            service.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();
            service.QueryLogCommonCompleted += new QueryLogCommonCompletedEventHandler(service_QueryLogCommonCompleted);
            BindOperaters();
            // List<QueryCondition> qclist = new List<QueryCondition>();
            dtpStartTime.Value = DateTime.Now;
            dtpEndTime.Value = DateTime.Now;

            QueryCondition qc = new QueryCondition();
            qc.StartTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            qc.EndTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            qc.UserID = "-1";
            BindDataGrid(qc);
            base.Onload();
        }


        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(dtpStartTime.Value.ToShortDateString()) > Convert.ToDateTime(Convert.ToDateTime(dtpEndTime.Value.ToShortDateString())))
            {
                MessageBox.Show("开始时间不能大于结束时间！");
                return;
            }
            QueryCondition qc = new QueryCondition();
            qc.StartTime = Convert.ToDateTime(dtpStartTime.Value.ToString("yyyy-MM-dd 00:00:00"));
            qc.EndTime = Convert.ToDateTime(dtpEndTime.Value.ToString("yyyy-MM-dd 23:59:59"));
            if (cmbOperater.Text == "未选择") { qc.UserID = "-1"; qc.UserName = "-1"; }
            else { qc.UserName = cmbOperater.Text; }// qc.UserID = cmbOperater.SelectedValue.ToString();
            BindDataGrid(qc);

        }





    }
}
